package com.qt.myio;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @Author zsf
 * @Date 2023/9/25 21:33
 * @Version 1.0
 */
public class FileSplitUtils {

    public static void main(String[] args) throws IOException {
        //源文件地址
        File sourceFile = new File("D:\\java测试目录\\api日志\\apusic_2023-09-20.log");
        //块文件目录
        String chunkFileFolder = "D:\\java测试目录\\api日志\\chunks";
        //块文件大小
        int chunkFileSize = 60 * 1024 * 1024;
        //块文件数量
        int chunkFileNum = (int) Math.ceil(sourceFile.length() * 1.0 / chunkFileSize);
        //
        RandomAccessFile readFile = new RandomAccessFile(sourceFile, "r");
        byte[] bytes = new byte[1024];
        for(int i = 0; i < chunkFileNum; i++) {
            File chunkFile = new File(chunkFileFolder  + "\\" + i + ".log");
            int len = -1;
            //创建块文件
            RandomAccessFile writeFile = new RandomAccessFile(chunkFile, "rw");
            while((len = readFile.read(bytes)) != -1) {
                writeFile.write(bytes, 0 ,len);
                //如果块文件大小达到1M，就读下一块
                if(chunkFile.length() >= chunkFileSize) {
                    break;
                }
            }
            writeFile.close();
        }
        readFile.close();
    }

}
